<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
    <style>
        body {
            background-color: #f4f4f4; /* 背景颜色 */
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .register-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); /* 阴影效果 */
        }

        h1 {
            text-align: center;
            margin-bottom: 20px;
        }

        form {
            display: flex;
            flex-direction: column;
        }

        label {
            margin-top: 10px;
        }

        input {
            margin-bottom: 15px;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        input[type="submit"] {
            background-color: #28a745;
            color: white;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #218838;
        }

        p {
            color: red;
            text-align: center;
            margin-top: 10px;
        }

        .login-link {
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h1>用户注册</h1>
    <form action="/register" method="post" onsubmit="return validateForm()">
        <label for="nickname">用户名:</label>
        <input type="text" id="nickname" name="nickname" required><br><br>
        <label for="id">ID:</label>
        <input type="text" id="id" name="id" required><br><br>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required><br><br>
        <label for="confirmPassword">确认密码:</label>
        <input type="password" id="confirmPassword" name="confirmPassword" required><br><br>
        <input type="submit" value="注册">

    </form>
    <p th:if="${error}" th:text="${error}"></p>
    <div class="login-link">
        <a href="/">已有账号？去登录-></a>
    </div>
</div>
<!-- 引入 Bootstrap JavaScript（可选，用于一些交互功能） -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
<script>
    async function validateForm() {
        var id = document.getElementById("id").value;
        var password = document.getElementById("password").value;
        var confirmPassword = document.getElementById("confirmPassword").value;

        try {
            // 发送 AJAX 请求检查 id 是否已存在
            const response = await fetch('/checkId?id=' + encodeURIComponent(id));
            const data = await response.json();
            if (data.exists) {
                alert("ID 已存在");
                return false;
            }
        } catch (error) {
            console.log(error);
            console.error("检查 ID 时出错:", error);
            alert("检查 ID 时出错，请稍后重试");
            return false;
        }

        if (password!== confirmPassword) {
            alert("请输入相同密码");
            return false;
        }

        return true;
    }
</script>
</body>
</html>